package com.example.goodpet.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.activity.TieziInfoActivity;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.community.bean.UserTieziInfo;
import com.example.goodpet.home.homeRecommend.adapter.RecommendRecycleViewAdapter;
import com.example.goodpet.service.HomeService;
import com.example.goodpet.utils.Constants;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Retrofit;

public class ResultTieziRecycleViewAdapter extends DelegateAdapter.Adapter<ResultTieziRecycleViewAdapter.ViewHolder>{
    private static final String TAG = RecommendRecycleViewAdapter.class.getSimpleName();
    private final Context mContext;
    public List<UserTieziInfo> datas;
    private Retrofit retrofit;
    private HomeService homeService;
    public ResultTieziRecycleViewAdapter(Context mContext, List<UserTieziInfo> infos) {
        this.mContext=mContext;
        this.datas=infos;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return null;
    }

    @NonNull
    @Override
    public ResultTieziRecycleViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend_recycle_view, parent, false);
        return new ResultTieziRecycleViewAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ResultTieziRecycleViewAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        //根据位置得到对应的数据
        UserTieziInfo info = datas.get(position);
//        Log.e(TAG, "onBindViewHolder: "+info );
        holder.tv_name.setText(info.getContent());
        //根据info的数据把宠物图片的第一张加载到相应的imageView上
        Glide.with(mContext)
                .load(Constants.BASE_URL_IMAGE+info.getTieziImage().get(0))
                .into(holder.iv_recommend);

        //根据info的数据初始化发布者信息
        Glide.with(mContext)
                .load(Constants.BASE_URL_IMAGE+info.getPortrait())
                .into(holder.iv_portrait);
        holder.tv_nickname.setText(info.getNickname());
        holder.tv_pet_location.setText("发布于"+info.getPostdate());

        holder.ly_item_recommend_recycle_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TieziInfoActivity.class);
                UserTieziInfo userTieziInfo = datas.get(position);
                intent.putExtra("userTieziInfo",userTieziInfo);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return  datas==null?0:datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_recommend;
        TextView tv_name;
        TextView tv_nickname;
        TextView tv_pet_location;
        CircleImageView iv_portrait;
        LinearLayout ly_item_recommend_recycle_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_recommend=itemView.findViewById(R.id.iv_recommend);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_nickname=itemView.findViewById(R.id.tv_nickname);
            tv_pet_location=itemView.findViewById(R.id.tv_pet_location);
            iv_portrait=itemView.findViewById(R.id.iv_portrait);
            ly_item_recommend_recycle_view=itemView.findViewById(R.id.ly_item_recommend_recycle_view);
        }
    }
}
